<template>
  <page-layout>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div>
      <a-form @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="12">
            <a-form-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input style="width:100%;" placeholder="请输入客户名称" v-model="queryParam.customerName"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <a-form-item>
              <a-button type="primary" @click="searchQuery()" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset()" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- table区域-begin -->
    <div>
      <vxe-grid
        id="FollowupCustomerList"
        keep-source
        ref="vxeTable"
        :height="null"
        v-bind="options"
        @checkbox-change="onSelectChange"
        @checkbox-all="onSelectChange"
        @page-change="handlePageChange"
      >
      <template #customerName="{ row }">
        <a @click="handleDetail(row)">{{ row.customerName }}</a>
      </template>
      <template #action="{ row }">
        <template>
          <a @click="showFollowup(row)">跟进记录</a>
          <a-divider type="vertical" />
          <a @click="handleDealStatus(row)">变更成交状态</a>
        </template>
      </template>
    </vxe-grid>
    </div>
    <!-- table区域-end -->
    <CrmCustomerModal ref="modalForm" @ok="modalFormOk"></CrmCustomerModal>
    <!-- 跟进记录功能 -->
    <crm-followup ref="followupForm" @close="modalFormOk"></crm-followup>
  </a-card>
  </page-layout>
</template>

<script>
import PageLayout from '@/layouts/PageLayout'
import { VxeTableMixin } from '@/mixins/VxeTableMixin'
import { deleteAction,httpAction } from '@/services/manage'
import ZDictSelectTag from '@/components/dict/ZDictSelectTag'
import CrmCustomerModal from '../customer/modules/CrmCustomerModal.vue'
import CrmFollowup from "../followup/CrmFollowup";

export default {
  name: 'FollowupCustomerList',
  components: {
    PageLayout,
    ZDictSelectTag,
    CrmCustomerModal,
    CrmFollowup
  },
  mixins: [VxeTableMixin],
  data() {
    return {
      columns: [
          {
            title:'客户名称',
            width:200,
            align:"center",
            slots: { default: 'customerName' },
          },
          {
            title:'客户类型',
            align:"center",
            field: 'customerType',
            formatter: 'dictText',
            width: 150
          },
          {
            title:'下次联系时间',
            align:"center",
            field: 'nextTime',
            width: 150
          },
          {
            title:'成交状态',
            align:"center",
            field: 'dealStatus',
            formatter: 'dictText',
            width: 100
          },
          {
            title:'手机',
            align:"center",
            field: 'mobile',
            width: 150
          },
          {
            title:'电话',
            align:"center",
            field: 'telephone',
            width: 150
          },
          {
            title:'网址',
            align:"center",
            showOverflow:true,
            field: 'website',
            width: 150
          },
          {
            title:'备注',
            align:"center",
            field: 'remark',
            width: 150
          },
          {
            title:'负责人',
            align:"center",
            field: 'ownerUserId',
            formatter: 'dictText',
            width: 150
          },
          {
            title:'上一次联系内容',
            align:"center",
            field: 'lastContent',
            width: 150
          },
          {
            title:'所属部门',
            align:"center",
            field: 'createDept',
            formatter: 'dictText',
            width: 150
          },
          {
            title:'创建人',
            align:"center",
            field: 'createBy',
            formatter: 'dictText',
            width: 150
          },
          {
            title:'创建时间',
            align:"center",
            field: 'createTime',
            width: 150
          },
          {
            title:'更新时间',
            align:"center",
            field: 'updateTime',
            width: 150
          },
          {
            title: '操作',
            field: 'action',
            align:"center",
            fixed:"right",
            width:200,
            slots: { default: 'action' }
          }
        ],
      url: {
		  list: "/customer/crmCustomer/listToday",
          delete: "/customer/crmCustomer/delete",
          deleteBatch: "/customer/crmCustomer/deleteBatch",
		  updateStatus: "/customer/crmCustomer/updateStatus",
          exportXlsUrl: "/customer/crmCustomer/exportXls",
          importExcelUrl: "customer/crmCustomer/importExcel",
      },
      // 查询参数
      queryParam: {},
      selectedRowKeys: [],
      selectedRows: [],
      toggleSearchStatus: false,
      customTableToolbar: {
        buttons: [],
      },
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    handleDelete: function(id) {
      if (!this.url.delete) {
        this.$message.error('请设置url.delete属性!')
        return
      }
      var that = this
      deleteAction(that.url.delete, { "id": id }).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
	handleDealStatus(row){
	  let formData = {id: row.id}
	  var that = this
	  httpAction(this.url.updateStatus, formData, 'put').then((res)=>{
	    if(res.success){
	      that.$message.success(res.message);
	    }else{
	      that.$message.warning(res.message);
	    }
	  }).finally(() => {
	    this.loadData(1)
	  })
	},
    showDetail(record){
      this.$refs.detailForm.open(record);
    },
    //跟进记录功能 ，自行修改category为自己的表名
    showFollowup(record){
      let category = 'crm_customer';
      this.$refs.followupForm.open(record,category);
    },
  }
}
</script>

